package it.polimi.TravelDream.ejb.eaos;

import java.util.ArrayList;
import java.util.List;

import it.polimi.TravelDream.ejb.dtos.AddressDTO;
import it.polimi.TravelDream.ejb.dtos.HotelDTO;
import it.polimi.TravelDream.ejb.dtos.RoomDTO;
import it.polimi.TravelDream.ejb.entities.Address;
import it.polimi.TravelDream.ejb.entities.BasicPackage;
import it.polimi.TravelDream.ejb.entities.Hotel;
import it.polimi.TravelDream.ejb.entities.Room;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class RoomEAO
 */
@Stateless
public class RoomEAO implements RoomList {

	@PersistenceContext
	private EntityManager em;
	
	private List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();
	List<Hotel> hotels = new ArrayList<Hotel>();
	
    /**
     * Default constructor. 
     */
    public RoomEAO() {
    }
    
	@Override
    public void add(RoomDTO roomDTO, HotelDTO hotelDTO) {
    	Room room = new Room(roomDTO);

		room.setHotelName(em.find(Hotel.class, hotelDTO.getHotelID()));
		em.persist(room);
		
		this.insertIntoOldPackages(room);
    }
	
	//Inserisco le nuove camere appena create nei pacchetti che condividono il periodo di validita e la citta di arrivo o di partenza
    private void insertIntoOldPackages(Room room) {
  		List<BasicPackage> listOfPackage = new ArrayList<BasicPackage>();
  		
  		listOfPackage = em.createNamedQuery(BasicPackage.FIND_ALL_AVAILABLE_PACKAGE, BasicPackage.class).getResultList();
  		for(BasicPackage p: listOfPackage) {
  			if( p.getCity().equals(room.getHotelName().getHotelAddress().getCity()) ) {
  				if( !p.getRooms().contains(room) ){
  					p.getRooms().add(room);
  				}
  			}
  		}
  		
  	}
    
	//Ricerca le citta che contengono un albergo
	@Override
    public List<HotelDTO> searchHotels() {
		this.hotels = em.createNamedQuery(Hotel.FIND_ALL_HOTEL, Hotel.class).getResultList();
		for(Hotel h: hotels) {
			hotelsDTO.add(convertHotelToDTO(h, convertAddressToDTO(h.getHotelAddress())));
		}
		return hotelsDTO;
    }
	
	private HotelDTO convertHotelToDTO(Hotel h, AddressDTO addressDTO){
    	HotelDTO hotelDTO = new HotelDTO();
    	hotelDTO.setName(h.getName());
    	hotelDTO.setStars(h.getStars());
    	hotelDTO.setAddressDTO(addressDTO);
    	return hotelDTO;
    }
    
    private AddressDTO convertAddressToDTO (Address a){
    	AddressDTO addressDTO = new AddressDTO();
    	addressDTO.setStreet(a.getStreet());
    	addressDTO.setStreetNumber(a.getStreetNumber());
    	addressDTO.setCity(a.getCity());
    	addressDTO.setCap(a.getCap());
    	addressDTO.setDistrict(a.getDistrict());
    	addressDTO.setNation(a.getNation());
    	return addressDTO;
    }

    @Override
	public List<HotelDTO> getHotelsDTO() {
		return hotelsDTO;
	}

    @Override
	public void setHotelsDTO(List<HotelDTO> hotelsDTO) {
		this.hotelsDTO = hotelsDTO;
	}  

}
